package com.changeover.gwt.client.view.task.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.changeover.gwt.client.view.task.ICustomerGridView;
import com.changeover.gwt.shared.datasource.ICustomerDataSource;
import com.changeover.gwt.shared.model.customer.ICustomer;
import com.changeover.gwt.shared.model.customer.LastAction;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.SelectionCell;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SafeHtmlHeader;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

public class CustomerGridView extends Composite implements ICustomerGridView {

    private final DataGrid<ICustomer> customerGrid;

    public CustomerGridView(ICustomerDataSource dataSource) {
        customerGrid = new DataGrid<ICustomer>();
        customerGrid.setSize("100%", "100%");

        TextColumn<ICustomer> idColumn = new TextColumn<ICustomer>() {
            @Override
            public String getValue(ICustomer customer) {
                return String.valueOf(customer.getId());
            }
        };

        TextColumn<ICustomer> nameColumn = new TextColumn<ICustomer>() {
            @Override
            public String getValue(ICustomer customer) {
                return String.valueOf(customer.getName());
            }
        };

        TextColumn<ICustomer> statusColumn = new TextColumn<ICustomer>() {
            @Override
            public String getValue(ICustomer customer) {
                return String.valueOf(customer.getStatus());
            }
        };

        List<String> lastActionNames = new ArrayList<String>();
        for (LastAction lastAction : dataSource.getLastActions()) {
            lastActionNames.add(lastAction.getName());
        }

        SelectionCell lastActionCell = new SelectionCell(lastActionNames);
        Column<ICustomer, String> lastActionColumn = new Column<ICustomer, String>(lastActionCell) {
            @Override
            public String getValue(ICustomer object) {
                return object.getLastAction().getName();
            }
        };

        DateTimeFormat format = DateTimeFormat.getFormat(("dd.MM.yy"));
        Column<ICustomer, Date> remainderColumn = new Column<ICustomer, Date>(new DateCell(format)) {
            @Override
            public Date getValue(ICustomer object) {
                return object.getRemainder();
            }
        };

        idColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        nameColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        statusColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        lastActionColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        remainderColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

        SafeHtmlHeader idHeader = new SafeHtmlHeader(new SafeHtml() {
            private static final long serialVersionUID = -5528872195423122982L;

            @Override
            public String asString() {
                return "<p style=\"text-align:center;\">ID</p>";
            }
        });

        SafeHtmlHeader nameHeader = new SafeHtmlHeader(new SafeHtml() {
            private static final long serialVersionUID = 3149024255926410430L;

            @Override
            public String asString() {
                return "<p style=\"text-align:center;\">Name</p>";
            }
        });

        SafeHtmlHeader statusHeader = new SafeHtmlHeader(new SafeHtml() {
            private static final long serialVersionUID = 7993745439538130406L;

            @Override
            public String asString() {
                return "<p style=\"text-align:center;\">Status</p>";
            }
        });

        SafeHtmlHeader lastActionHeader = new SafeHtmlHeader(new SafeHtml() {
            private static final long serialVersionUID = -1508644670847898987L;

            @Override
            public String asString() {
                return "<p style=\"text-align:center;\">Last Action</p>";
            }
        });

        SafeHtmlHeader remainderHeader = new SafeHtmlHeader(new SafeHtml() {
            private static final long serialVersionUID = 8748568624794885560L;

            @Override
            public String asString() {
                return "<p style=\"text-align:center;\">Remainder</p>";
            }
        });

        customerGrid.addColumn(idColumn, idHeader);
        customerGrid.addColumn(nameColumn, nameHeader);
        customerGrid.addColumn(statusColumn, statusHeader);
        customerGrid.addColumn(lastActionColumn, lastActionHeader);
        customerGrid.setColumnWidth(lastActionColumn, "220px");
        customerGrid.addColumn(remainderColumn, remainderHeader);

        customerGrid.setRowCount(dataSource.getCustomers().size(), true);
        customerGrid.setRowData(dataSource.getCustomers());

        initWidget(customerGrid);
    }

}
